<template>
  <div class="login-container">
    <div class="login-content">
      <div class="logo">
        <img src="@/assets/log_icon.png" alt="Logo" />
      </div>
      <div class="login-form">
        <h2>xxxxxxxx</h2>
        <div class="form-group">
          <label for="username">
            <i class="fas fa-user"></i>
            <el-input id="username" v-model="loginForm.username" placeholder="用户名" clearable></el-input>
          </label>
        </div>
        <div class="form-group">
          <label for="password">
            <i class="fas fa-lock"></i>
            <el-input id="password" type="password" v-model="loginForm.password" placeholder="密码" clearable show-password></el-input>
          </label>
        </div>
        <div class="button-container">
          <el-button type="primary" class="submit-button" @click="login">登录</el-button>
        </div>

      </div>
    </div>
  </div>
</template>
<script setup>
import { reactive } from 'vue'
import { loginAPI } from '@/api/index'
import { useAuthStore } from '@/stores/auth'
import { setToken } from '@/utils/auth'
import { useRouter } from 'vue-router'
import { useRoutesStore } from '@/stores/routes'
import { ElMessage } from 'element-plus'
const loginForm = reactive({
  username: 'admin',
  password: '123',
})
const authStore = useAuthStore()
const router = useRouter()
const routesStore = useRoutesStore()

async function login() {
  const params = { ...loginForm }
  if (!params.username) {
    ElMessage({
      message: '请输入账号/密码',
      type: 'warning'
    })
    return
  }
  const res = await loginAPI(params)
  if (res.code === 200 && res.data) {
    console.log(res)

    // 登录成功，存储 token
    authStore.login(res.data.token)
    setToken(res.data.token)
    // 获取权限路由列表与存储
    const isSetRoutes = await routesStore.setRoutes()
    if (isSetRoutes) {
     
      router.push('/')
    }
  } else {
    // 登录失败，显示错误信息  
    
    authStore.logout()
  }
}
</script>


<style scoped lang="scss">
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: #67BEEB; // Light blue background
}

.login-content {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background-color: #ffffff;
  padding: 40px;
  border-radius: 10px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  width: 400px;
}

.logo {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-bottom: 20px;
}

.logo img {
  width: 100px;
  height: 100px;
  border-radius: 50%;
}

.login-form {
  width: 100%;
}

h2 {
  text-align: center;
  color: #333333;
  font-size: 24px;
  margin-bottom: 20px;
}

.form-group {
  margin-bottom: 20px;
  position: relative;
}

label {
  display: flex;
  align-items: center;
  color: #666666;

  i {
    margin-right: 10px;
  }
}
.button-container {
  display: flex;
  justify-content: flex-end;
}

</style>
